package handlers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"new/models"
)

func Add(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	user := models.User{
		Username: username,
		Password: password,
	}
	err := models.DB.Create(&user).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "用户注册失败",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "用户注册成功",
		})
		return
	}
}

func List(c *gin.Context) {
	var news models.News
	err := models.DB.Find(&news).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "登录失败",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "登录成功",
		})
		return
	}
}

func NewsAdd(c *gin.Context) {
	user := c.PostForm("user")
	newspaper := c.PostForm("newspaper")
	userDz := c.PostForm("userDz")
	userGz := c.PostForm("userGz")
	news := models.News{
		User:      user,
		Newspaper: newspaper,
		UserDz:    userDz,
		UserGz:    userGz,
	}
	err := models.DB.Create(&news).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "发布失败",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "发布成功",
		})
		return
	}
}
func NewsList(c *gin.Context) {
	var news models.News
	err := models.DB.Find(&news).Error
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 0,
			"msg":  "查询失败",
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "查询成功",
		})
		return
	}
}
